const book = {
  state: {
    bookId: "",
    bookName: "",
    chapterId: "",
    chapterName: "",
    contentData: {},
    refreshBook: 0,
    refreshChapter: 0,
    refreshContent:0
  },
  mutations: {
    SET_REFRESH_BOOK: (state, refresh) => {
      if (refresh) state.refreshBook++;
    },

    SET_CONTENT_DATA: (state, data) => {
      console.info(data)
      state.refreshContent++;
      if(!data)data = {id:0}
      state.contentData = data;
    },
    SET_REFRESH_CHAPTER: (state, refresh) => {
      if (refresh) state.refreshChapter++;
    },
    SET_BOOK: (state, book) => {
      state.bookId = book.id;
      state.bookName = book.bookName;
    },
    SET_CHAPTER: (state, chapter) => {
      state.chapterId = chapter.id;
      state.chapterName = chapter.chapterName;
    }
  },
  actions: {
    DoRefreshChapter({
      commit
    }, refresh) {
      commit('SET_REFRESH_CHAPTER', refresh);
    },

    DosetContentData({
      commit
    }, data) {
      commit('SET_CONTENT_DATA', data);
    },
    
    DoRefreshBook({
      commit
    }, refresh) {
      commit('SET_REFRESH_BOOK', refresh);
    },
    DoSetBook({
      commit
    }, book) {
      book = book ? book : getDefaultBook();
      commit('SET_BOOK', book);
      commit('SET_CHAPTER', getDefaultChapter());
    },
    DoSetChapter({
      commit
    }, chapter) {
      chapter = chapter ? chapter : getDefaultChapter();
      commit('SET_CHAPTER', chapter);

    }
  }
}

function getDefaultBook() {
  return {
    id: "",
    bookName: "",
  }
}

function getDefaultChapter() {
  return {
    id: "",
    chapterName: "",
  }
}
export default book
